<template>
  <div class="update-gender">
    <van-picker title="标题"
                show-toolbar
                :columns="columns"
                :default-index="value"
                @confirm="onConfirm"
                @cancel="this.$emit('close')"
                @change="onPickerChange" />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      localGender: this.value
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中……',
        forbidClick: true,
        duration: 0
      })
      try {
        await updateUserProfile({
          gender: this.localGender
        })
        this.$emit('input', this.localGender)
        this.$emit('close')
        this.$toast('更新成功')
      } catch (err) {
        this.$toast('更新失败')
      }
    },
    onPickerChange (piker, value, index) {
      this.localGender = index
    }
  }
}
</script>

<style>
</style>
